<?php

function success($msg){
	echo  "{success:true,msg:'$msg'}";
	}
function failure($msg){
	echo  "{failure:true,msg:'$msg'}";
	}

function result($total,$result){
	echo '{"total":"'.$total.'","results":'.json_encode($result).'}';
	}

function rands($size) {
	$num="";
	for($i=0;$i<$size;$i++) {
		$num.= rand(0,9);
	}
	return $num;
}

function charReplace($str) {
	$str = str_replace('&','',$str);
	//$str = str_replace(' ','',$str);
	$str = str_replace(',',' ',$str);
	return $str;
}

function dhtmlspecialchars($string) { 
	if(is_array($string)) { 
		foreach($string as $key => $val) { 
			$string[$key] = dhtmlspecialchars($val); 
		} 
	} else { 
		//$string = preg_replace("@<(.*?)>@is","",$string);
		$string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1', 
		$string = str_replace(array("\r",chr(10),'&', '"', '<', '>','&nbsp;','é','Nü','（','）','#','amp;','160;'), '', $string)); 
	} 
	return $string; 
}

function htmlFilter($str) {
	return preg_replace("@<(.*?)>@is","",$str);
}

function numberToArray($start,$end) {
	for(;$start <= $end;$start++) {
		$arr[] = $start;
	}
	return $arr;
}

function multiDir($dirs) {
    if (strpos($dirs,"/") !==false){
    	//$tmpArr = getStringToArray($dirs,"/");
		$tmpArr = explode('/',$dirs);
		
    	foreach ($tmpArr as $tmpDir) {
    		$_dir.= $tmpDir."/";
    		if (!file_exists($_dir)) {
    			mkdir($_dir,0777);
    		}
    	}
    } else {
    	if (!file_exists($dirs))
			mkdir($dirs,0777);
    }
}

function getImage($url,$filename="") {
	if(!$url) return false;
	if(!$filename) {
		$ext = strrchr(strtolower($url),".");
		if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
		//$filename="./".$dir."/".date("dMYHis").$ext;
	}
	ob_start();
	readfile($url);
	$img = ob_get_contents();
	ob_end_clean();
	
	$fp2=@fopen($filename, "wb");
	fwrite($fp2,$img);
	fclose($fp2);
  return $filename;
}

function arrayToStr($arr,$deno=',') {
	if ($arr) {
		foreach ($arr as $_val) {
			$str .= $_val . $deno;
		}
		$str = trim($str,$deno);
		return $str;
	}
}
?>